/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package ua.i.kotionokyurievna.jewelryweb.controllers;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import ua.i.kotionokyurievna.jewelryweb.entities.Gender;
import ua.i.kotionokyurievna.jewelryweb.entities.Person;
import ua.i.kotionokyurievna.jewelryweb.facade.PersonFacade;
import ua.i.kotionokyurievna.jewelryweb.util.PasswordEncryptor;


/**
 *
 * @author kotionokyurievna
 */
@ManagedBean
@RequestScoped
public class RegistrationPageController {

    private String login;
    private String password;
    private String passwordConfirm;
    private String name;
    private String surname;
    private Date birthDate;
    private Gender gender;
    private String telephone;
    private String email;
    
    @EJB 
    PersonFacade personFacade;
    PasswordEncryptor encryptor;

    public RegistrationPageController() {
        this.encryptor = new PasswordEncryptor();
    }

    public RegistrationPageController(String login, String password, 
            String passwordConfirm, String name, String surname, 
            String telephone, String email, int agreeLabel) {
        this.login = login;
        this.password = password;
        this.passwordConfirm = passwordConfirm;
        this.name = name;
        this.surname = surname;
        this.telephone = telephone;
        this.email = email;
        this.encryptor = new PasswordEncryptor();
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public String getPasswordConfirm() {
        return passwordConfirm;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setPasswordConfirm(String passwordConfirm) {
        this.passwordConfirm = passwordConfirm;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Date getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }

    public Gender getGender() {
        return gender;
    }

    public void setGender(Gender gender) {
        this.gender = gender;
    }
    
    public String registerUser(){
        if(password.equals(passwordConfirm)) {
            String ePassword = encryptor.encrypt(password);
            personFacade.save(new Person(name, surname, birthDate,
                    telephone, email, gender, login, ePassword));
            return "loginPage";
        }
        FacesContext.getCurrentInstance().addMessage(
        null,
        new FacesMessage(FacesMessage.SEVERITY_WARN,
        "Passwords does not match", null));
        return "registrationPage";
    }
    
}
